from utils.ApiCryptoUtils import api_crypto_utils
from web_server_function.ServerUtils import server_utils
from utils.ReadUtil import read_util
from UserClient import userClient
from utils.log_util import logger
import time


class CourseClient:

    def __init__(self, platform_name, appV):
        # 设备号
        self.deviceid = server_utils.generate_random_string(rando_name="", length=32)
        # 当前时间戳毫秒
        self.ctime = int(time.time() * 1000)
        # app版本号
        self.build = "1.1.9"
        # app系统 ANDRIOD/IOS
        self.platform_name = platform_name
        # app包 BOOKS/XKB
        self.appV = appV

    def findMyCourse(self, class_id, return_full_resp=False, index1=0, index2=0):
        """
        查询普通课程，根据年级ID
        """
        try:
            data_dict = {"appUUId": read_util.read_data()['app_uid'],
                         "deviceId": self.deviceid, "type": 1, "build": self.build,
                         "classId": class_id, "page": "1",
                         "token": read_util.read_data()['app_token'],
                         "platformName": self.platform_name, "appV": self.appV}
            resp = api_crypto_utils.app_post_request(url=f"/v1/orders/findMyCourse",
                                                     data=data_dict)
            if return_full_resp:
                return resp
            ordersId = resp['appData']['courseList'][index1]['ordersId']
            rsNo = resp['appData']['courseList'][index2]['courseInformationId']
            return ordersId, rsNo

        except Exception as e:
            server_utils.handle_exception(e, "查询普通课程失败")

    def paperCourseList(self, grade_id, return_full_resp=False, index1=0):
        """
        查询纸上课堂课程，根据年级ID
        """
        try:
            data_dict = {"appUUId": read_util.read_data()['app_uid'],
                         "deviceId": self.deviceid, "build": self.build, "pageSize": 15,
                         "subjectId": "", "page": 1,
                         "token": read_util.read_data()['app_token'],
                         "gradeId": grade_id, "platformName": self.platform_name, "appV": self.appV}
            resp = api_crypto_utils.app_post_request(url=f"/v1/paperCourse/list",
                                                     data=data_dict)
            if return_full_resp:
                return resp
            return resp['appData']['userInfoPaperCourseList'][index1]['id']
        except Exception as e:
            server_utils.handle_exception(e, "查询纸上课堂课程失败")

    def courseFindByCourseId(self, orders_id, course_information_id):
        """
        查询课程详情，根据订单号与课程信息ID
        """
        try:
            data_dict = {"courseListFor": "1",
                         "appUUId": read_util.read_data()['app_uid'],
                         "deviceId": self.deviceid,
                         "ordersId": orders_id,
                         "courseType": "1", "purType": "1",
                         "token": read_util.read_data()['app_token'],
                         "page": "1", "platformName": self.platform_name, "appV": self.appV, "sweepClaimCourse": "0",
                         "build": self.build, "opType": "0",
                         "rsNo": course_information_id}
            api_crypto_utils.app_post_request(url=f"/v1/course/findByCourseId",
                                              data=data_dict)
        except Exception as e:
            server_utils.handle_exception(e, "查询课堂详情失败")

    def paperCourseDetail(self, id):
        """
        查询纸上课堂详情，根据ID
        """
        try:
            data_dict = {"appUUId": read_util.read_data()['app_uid'],
                         "deviceId": self.deviceid, "build": self.build,
                         "token": read_util.read_data()['app_token'],
                         "platformName": self.platform_name, "appV": self.appV, "id": id}
            api_crypto_utils.app_post_request(url=f"/v1/paperCourse/detail",
                                              data=data_dict)
        except Exception as e:
            server_utils.handle_exception(e, "查询纸上课堂课堂详情失败")


courseClient = CourseClient(platform_name="ANDRIOD", appV="BOOKS")

if __name__ == "__main__":
    logger.info("登录开始")
    userClient.user_login("13500000000", "Xkb87654312")
    logger.info("登录结束\n")

    logger.info("查询高中普通课程开始")
    ordersId, rsNo = courseClient.findMyCourse("F6DB6072D21DEFEF")
    logger.info("查询高中普通课程结束\n")

    logger.info("查询高中普通课程详情开始")
    courseClient.courseFindByCourseId(ordersId, rsNo)
    logger.info("查询高中普通课程详情结束\n")

    logger.info("查询高中纸上课堂开始")
    cid = courseClient.paperCourseList("F6DB6072D21DEFEF")
    logger.info("查询高中纸上课堂结束\n")

    logger.info("查询高中纸上课堂详情开始")
    courseClient.paperCourseDetail(cid)
    logger.info("查询高中纸上课堂详情结束\n")

    # logger.info("查询初中普通课程开始")
    # courseClient.findMyCourse("FDEB9B4D17979C53")
    # logger.info("查询初中普通课程结束\n")

    # logger.info("查询初中纸上课堂开始")
    # courseClient.paperCourseList("FDEB9B4D17979C53")
    # logger.info("查询初中纸上课堂结束\n")
